package com.bj.dljd.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bj.dljd.PowerBoosApplication;
import com.bj.dljd.domain.PayProduct;
import com.bj.dljd.domain.PayWay;
import com.bj.dljd.domain.UserPayConfig;
import com.bj.dljd.enums.PublicStatus;
import com.bj.dljd.excepyions.BossException;
import com.bj.dljd.mapper.PayProductMapper;
import com.bj.dljd.mapper.PayWayMapper;
import com.bj.dljd.mapper.UserPayConfigMapper;
import com.bj.dljd.models.dic.DicItem;
import com.bj.dljd.models.query.ProductConfigQuery;
import com.bj.dljd.models.query.ProductQuery;
import com.bj.dljd.models.response.R;
import com.bj.dljd.service.PaymentService;
import com.bj.dljd.util.IdUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class PaymentServiceImpl implements PaymentService {

    @Resource
    private PayProductMapper payProductMapper;
    @Resource
    private PayWayMapper payWayMapper;

    @Resource
    private UserPayConfigMapper userPayConfigMapper;

    /**
     * 要求productCode 是唯一的
     * productName 是唯一的
     *
     * @param productQuery
     */
    @Override
    public synchronized void addProduct(ProductQuery productQuery) {
        // 要求productCode 是唯一的
        PayProduct productCode = payProductMapper.selectOne(new QueryWrapper<PayProduct>()
                .eq("product_code", productQuery.getProductCode()));
        if (productCode != null) {
            throw new BossException(R.fuil("支付产品编码已存在"));
        }
        // productName 是唯一的
        PayProduct productName = payProductMapper.selectOne(new QueryWrapper<PayProduct>()
                .eq("product_name", productQuery.getProductName()));
        if (productName != null) {
            throw new RuntimeException("支付产品名字已存在");
        }
        // 创建支付产品
        PayProduct payProduct = new PayProduct();
        payProduct.setId(IdUtil.id());
        payProduct.setProductCode(productQuery.getProductCode());
        payProduct.setProductName(productQuery.getProductName());
        payProduct.setStatus(PublicStatus.DISABLE.name());
        payProduct.setEditTime(new Date());
        payProduct.setRemark("创建支付产品");
        payProductMapper.insert(payProduct);

    }

    @Override
    public Page<PayProduct> productPageList(Integer pageNo, Integer pageSize) {
        return payProductMapper.selectPage(Page.of(pageNo, pageSize),
        new QueryWrapper<PayProduct>()
                .orderByDesc("edit_time"));
    }

    @Override
    public List<DicItem> getProductDicList() {
        List<DicItem> dicItemList = payProductMapper.getProductDicList();
        return dicItemList;
    }

    @Override
    public List<DicItem> getPayWayDicList() {
        return PowerBoosApplication.PAY_WAY_DIC_LIST;
    }

    @Override
    public List<DicItem> getPayTypeDicList(String wayCode) {
        // 从map获取key对应的value
        if (PowerBoosApplication.PAY_TYPE_DIC_MAP.containsKey(wayCode)){
            return PowerBoosApplication.PAY_TYPE_DIC_MAP.get(wayCode);
        }
        return new ArrayList<DicItem>();

    }

    @Override
    public void payTypeConfig(ProductConfigQuery productConfigQuery) {
        QueryWrapper<PayWay> wrapper = new QueryWrapper<>();
        wrapper.eq("pay_product_code",productConfigQuery.getProductCode())
                .eq("way_code",productConfigQuery.getWayCode())
                .eq("type_code",productConfigQuery.getTypeCode());
        PayWay payWay = payWayMapper.selectOne(wrapper);
        if (payWay != null) {
            throw new BossException(R.fuil("支付产品编码已存在"));
        }
        // 添加pay_way
        payWay = new PayWay();
        payWay.setId(IdUtil.id());
        payWay.setWayCode(productConfigQuery.getWayCode());
        payWay.setWayName(PowerBoosApplication.NAME_PAY_WAY.get(productConfigQuery.getWayCode()));
        payWay.setTypeCode(productConfigQuery.getTypeCode());
        payWay.setTypeName(PowerBoosApplication.NAME_PAY_TYPE.get(productConfigQuery.getWayCode()+":"+productConfigQuery.getTypeCode()));
        payWay.setPayProductCode(productConfigQuery.getProductCode());
        payWay.setPayRate(productConfigQuery.getPayRate());
        payWay.setSorts(productConfigQuery.getSorts());
        payWay.setStatus(PublicStatus.ENABLE.name());
        payWay.setEditTime(new Date());
        payWay.setRemark("创建了产品的支付方式配置");
        payWayMapper.insert(payWay);

    }

    @Override
    public Page<PayWay> pageWayPageList(String productCode , Integer pageNo, Integer pageSize) {
        QueryWrapper<PayWay> wrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(productCode)){
            wrapper.eq("pay_product_code",productCode);
        }
        wrapper.orderByAsc("pay_product_code","way_code","sorts");
        return payWayMapper.selectPage(Page.of(pageNo,pageSize),wrapper);
    }

    @Override
    public void editProductStatus(String productCode) {
        // 查询产品记录
        PayProduct payProduct = payProductMapper.selectOne(new QueryWrapper<PayProduct>()
                .eq("product_code", productCode));
        if (payProduct == null) {
            throw new BossException(R.fuil("支付产品编码不存在"));
        }
            // 修改产品状态
            String name = PublicStatus.ENABLE.name();
            if (PublicStatus.ENABLE.name().equals(payProduct.getStatus())) {
                name = PublicStatus.DISABLE.name();
            }
        PayProduct product = new PayProduct();
        product.setId(payProduct.getId());
        product.setStatus(name);
        payProductMapper.updateById(product);
    }

    /*删除支付产品*/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeProduct(String productCode) {
        QueryWrapper<UserPayConfig> wrapper = new QueryWrapper<>();
        wrapper.eq("pay_product_code",productCode);
        Long configCount = userPayConfigMapper.selectCount(wrapper);
        if (configCount > 0) {
            // 支付产品有使用的用户 不能删除
            throw new BossException(R.fuil("支付产品有使用的用户 不能删除"));
        }
        // 删除产品表
        int delete  = payProductMapper.delete(new QueryWrapper<PayProduct>().eq(
                "product_code", productCode));
        if (delete > 0) {
            // 删除支付方式配置
            payWayMapper.delete(new QueryWrapper<PayWay>()
                    .eq("pay_product_code", productCode));
        }
    }



    @Override
    public List<PayProduct> matchProductMatchName(String productName) {
        QueryWrapper<PayProduct> wrapper = new QueryWrapper<>();
        wrapper.like("product_name",productName)
                .orderByAsc("product_name");
        return payProductMapper.selectList(wrapper);
    }
}
